package com.allin.social.util;

import java.util.HashMap;
import java.util.Map;

/**
 * json字符串，动态解析（根据json有多少个字段，动态保存到map集合里面）
 *
 * @author lyq
 * @date 2015-4-14
 */
public class WebFrontUtil {


    /**
     * 分页参数转换 将pageIndex、pageIndex 转换成 firstResult、maxResult
     *
     * @param paramMap ()
     */
    public static void handlePage(Map paramMap) {
        int pageIndex = 0;
        int pageSize = 0;
        String pageIndexString = paramMap.get("pageIndex") != null ? paramMap.get("pageIndex").toString() : "1";
        String pageSizeString = paramMap.get("pageSize") != null ? paramMap.get("pageSize").toString() : "10";
        if (pageIndexString.length() > 0) {
            pageIndex = Integer.valueOf(pageIndexString);
        } else {
            pageIndex = 1;
        }
        if (pageSizeString.length() > 0) {
            pageSize = Integer.valueOf(pageSizeString);
        } else {
            pageSize = 10;
        }
        int firstResult = (pageIndex - 1) * pageSize;
        int maxResult = pageSize;
        paramMap.put("firstResult", firstResult);
        paramMap.put("maxResult", maxResult);
    }

    /**
     * 获取响应基本对象
     *
     * @param jsonData
     * @return
     */
    public static BaseResponseObject getResponseObject(String jsonData) {
        BaseResponseObject baseResponseObject = new BaseResponseObject(false, "", "");
        try {
            if (StringUtil.isNotEmpty(jsonData)) {
                baseResponseObject = (BaseResponseObject) GsonUtil.toBaseResponseObject(jsonData);
            }
        } catch (Exception ex) {
            LogUtil.e("---->execute exception:", ex.getMessage());
            ex.printStackTrace();
        }
        return baseResponseObject;
    }

    /**
     * 获取响应数据
     *
     * @param jsonData
     * @return
     */
    public static Map getResponseData(String jsonData) {
        Map data = new HashMap();
        BaseResponseObject baseResponseObject = getResponseObject(jsonData);
        if (baseResponseObject != null) {
            data = baseResponseObject.getResponseData();
        }
        return data;
    }

    /**
     * 获取响应数据_动态信息
     *
     * @param jsonData
     * @return
     */
    public static Object getResponseData(String jsonData, String property) {
        Object result = "";
        try {
            BaseResponseObject baseResponseObject = getResponseObject(jsonData);
            if (baseResponseObject != null) {
                Map data = baseResponseObject.getResponseData();
                if (data != null && StringUtil.isNotEmpty(property) && data.get(property) != null) {
                    result = data.get(property);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


}
